package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.contract.PwSetContract;
import com.bocai.kelalacar.ui.car.set.module.PwSetModule;
import com.bocai.kelalacar.ui.car.set.presenter.PwSetPresenter;
import com.bocai.kelalacar.util.SP;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/20.
 */

public class PwSetFirstActivity extends BaseActivity implements PwSetContract.view{
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_getcode)
    Button btnGetcode;
    @BindView(R.id.btn_next)
    Button btnNext;
    @Inject
    PwSetPresenter presenter;
    String phone;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    private String code;

    @Override
    public int getLayoutId() {
        return R.layout.act_pw_set_first;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new PwSetModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("密码设置");
        initBack(imgBack,llBack);
        phone = SP.get(mContext,"PhoneNumber","").toString().trim();
        String bb = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        tvPhone.setText(bb);
        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length()>=4){
                    btnNext.setEnabled(true);
                    btnNext.setTextColor(getResources().getColor(R.color.color_white));
                }else {
                    btnNext.setEnabled(false);
                    btnNext.setTextColor(getResources().getColor(R.color.text_color));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    @OnClick({R.id.btn_getcode,R.id.btn_next})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_getcode:
                presenter.sendCode(SP.get(mContext,"PhoneNumber","").toString().trim(),"1");
                break;
            case R.id.btn_next:
                if (TextUtils.isEmpty(etCode.getText().toString().trim())){
                    showShortToast("请输入验证码");
                }else {
                    if (code.equals(etCode.getText().toString().trim())) {
                        Intent intent = new Intent(PwSetFirstActivity.this, PwSetSecondActivity.class);
                        intent.putExtra("code", etCode.getText().toString().trim());
                        intent.putExtra("phone", SP.get(mContext, "PhoneNumber", "").toString().trim());
                        startActivity(intent);
                        finish();
                    }else {
                        showErrorTip("验证码错误，请重新输入");
                        etCode.setText("");
                    }
                }
                break;
        }
    }

    @Override
    public void getCode(String code) {
        this.code = code;
        presenter.setCode();
    }

    @Override
    public void resetPw() {

    }

    @Override
    public void setBtnCode(int index) {
        if (index == 0) {
            btnGetcode.setClickable(true);
            btnGetcode.setEnabled(true);
            btnGetcode.setText(getResources().getText(R.string.get_code));
        } else {
            btnGetcode.setClickable(false);
            btnGetcode.setEnabled(false);
            btnGetcode.setText(index + "秒");
        }
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }
}
